package com.study.spring.netty.time_client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author zhang
 * @version 1.0
 * @date 2019/5/7 0007
 * @since jdk1.8
 */
public class TimeClientHandler extends ChannelInboundHandlerAdapter {

    private static final Logger LOGGER = LoggerFactory.getLogger(TimeClientHandler.class);

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        LOGGER.info("收到的消息是:{}", msg);
//        ByteBuf  byteBuf = (ByteBuf) msg;
//        if (byteBuf.readableBytes() >= 17) {
//
//            byte[] bytes = new byte[17];
//
//            byteBuf.readBytes(bytes);
//            String body = new String(bytes,StandardCharsets.UTF_8);
//
//            LOGGER.info("收到的消息是:{}",body);
////            byteBuf.release();
//        }

//        ctx.close();

    }


    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        LOGGER.info("远程服务器主机{}", ctx.channel().remoteAddress());

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        LOGGER.info("远程服务器主机{}关闭channel", ctx.channel().remoteAddress());
//        ctx.close();
    }
}
